$(function() {
	$("#accountmodel-username").bind('input propertychange', function() {
		var phone = $('#accountmodel-username').val();
		if(checkMobile(phone)) {
			$('#accountmodel-username-error').hide();
			$.ajax({
				type: 'post',
				url: '/user/forget_pwd',
				data: {
					phone: phone
				},
				dataType: 'json',
				success: function(res) {
					if(res.code == 0) {
						$('#accountmodel-username-error').html('<i class="fa fa-warning"></i>' + res.message);
						$('#accountmodel-username-error').show();
					}
					if(res.code == 1) {
						$('#btn_submit').removeClass('disable');
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					showErrorMsg('网络失败，请刷新页面后重试');
				}
			})

		} else {
			$('#accountmodel-username-error').html('<i class="fa fa-warning"></i>请输入正确的手机号');
			$('#accountmodel-username-error').show();
		}
	})

	$('.iconfont').click(function() {
		$('#accountmodel-username').val('');
		$('#btn_submit').addClass('disable');
	})
	$('#btn_submit').click(function() {
		var disable = $('#btn_submit').hasClass('disable');
		if(!disable) {
			window.location = "/user/forget_pwd/step/2.html";
		}
	})
	$('#btn_submit2').click(function() {
		var disable = $('#btn_submit2').hasClass('disable');
		if(!disable) {
			window.location = "/user/forget_pwd/step/3.html";
		}
	})
	$('#btn_submit3').click(function() {
		var disable = $('#btn_submit3').hasClass('disable');
		if(!disable) {
			var pwd = $("#pwdInput").val();
			$.ajax({
				type: 'post',
				url: '/user/forget_pwd_rest',
				data: {
					pwd: pwd
				},
				dataType: 'json',
				success: function(res) {
					if(res.code==0){
						layer.open({
							content: res.message,
							skin: 'msg',
							time: 2 //2秒后自动关闭
						});
					}
					if(res.code==1){
						layer.open({
							content: res.message,
							skin: 'msg',
							time: 2 //2秒后自动关闭
						});
						setTimeout('window.location = "/user/login.html";', 1000);
					}
					
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					showErrorMsg('网络失败，请刷新页面后重试');
				}
			})
		}
	})

	var flag = true;
	$('#get_phonecode').click(function() {
		if(flag) {
			flag = false;
			$.post('/user/forget_pwd_moblie', {
				type: 1
			}, function(res) {
				if(res.code == 0) {
					layer.open({
						content: res.message,
						skin: 'msg',
						time: 2 //2秒后自动关闭
					});
					setTimeout('window.location = "/user/forget_pwd/step/1.html";', 1000);

					return false;
				} else {
					var second = 60;
					var timer = null;
					timer = setInterval(function() {
						second -= 1;
						if(second > 0) {
							$(this).attr("disabled", "true");
							$('.phonecode').html(second + 's后重发');
						} else {
							flag = true;
							clearInterval(timer);
							second = 60;
							$('.phonecode').html('获取手机验证码');
							$(this).attr("disabled", "false");
						}
					}, 1000);
					layer.open({
						content: res.message,
						skin: 'msg',
						time: 2 //2秒后自动关闭
					});

				}
			}, "json");
		}
	})
	$("#accountmodel-sms_captcha").bind('input propertychange', function() {
		var sms_captcha = $('#accountmodel-sms_captcha').val();
		if(sms_captcha.length == 4) {
			$('#accountmodel-username-error').hide();
			$.ajax({
				type: 'post',
				url: '/user/forget_pwd_yanzheng',
				data: {
					sms_captcha: sms_captcha
				},
				dataType: 'json',
				success: function(res) {
					if(res.code == 0) {
						$('#accountmodel-sms_captcha-error').html('<i class="fa fa-warning"></i>' + res.message);
						$('#accountmodel-sms_captcha-error').show();
					}
					if(res.code == 1) {
						$('#accountmodel-sms_captcha-error').hide();
						$('#btn_submit2').removeClass('disable');
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					showErrorMsg('网络失败，请刷新页面后重试');
				}
			})
		}
	})

	$("#pwdInput").bind('input propertychange', function() {
		var pwd = $("#pwdInput").val();
		var pass = /^[^\u4e00-\u9fa5\·]{6,20}$/;
		if(pass.test(pwd)) {
			console.log('ok');
			$(this).removeClass('error');
			$('#pwdInput-error').hide();
			$('#btn_submit3').removeClass('disable');
		} else {
			$('#pwdInput-error').html('<i class="fa fa-warning"></i>新密码长度为6个字符-20字符。');
			$('#pwdInput-error').show();
			$('#btn_submit3').addClass('disable');
		}
	});
})

function checkMobile(tel) {
	var reg = /(^1[3|4|5|7|8][0-9]{9}$)/;
	if(reg.test(tel)) {
		return true;
	} else {
		return false;
	};
}